package sr

import (
	"fmt"
	"os"
	"wails/modules/editor/configs"
	"wails/modules/editor/models"

	"gitee.com/zhongguo168a/gocodes/datax/schemax/configx"
	"gitee.com/zhongguo168a/gocodes/myx/errorx"
)

type Services struct {
}

func InitEditor() (err error) {
	err = configs.LoadCLocal()
	if err != nil {
		return err
	}

	set, errs := File.GetFileConfigAllMap()
	_ = errs

	configRepo := configx.NewConfigRepo()
	configRepo.Load(set)
	//
	configs.KeyToConfig.Set("图集", configRepo.GetBy编号("图集_默认"))
	configs.KeyToConfig.Set("图标", configRepo.GetBy编号("图标_默认"))
	configs.KeyToConfig.Set("预设", configRepo.GetBy编号("预设_默认"))
	configs.KeyToConfig.Set("音乐", configRepo.GetBy编号("音乐_默认"))
	configs.KeyToConfig.Set("音效", configRepo.GetBy编号("音效_默认"))
	configs.KeyToConfig.Set("纹理", configRepo.GetBy编号("纹理_默认"))
	configs.KeyToConfig.Set("地块", configRepo.GetBy编号("地块_图片_默认"))
	configs.KeyToConfig.Set("装饰品", configRepo.GetBy编号("装饰品_默认"))
	configs.KeyToConfig.Set("模型", configRepo.GetBy编号("模型_默认"))

	{
		err = initFileDir()
		if err != nil {
			return errorx.Wrap(err, "init file dir")
		}
	}

	return nil
}

func initFileDir() (err error) {
	configs.X系统文件夹 = []string{
		"config/core",
		"config/business",
		"config/world",
		"config/asset",
		"config/texture",
		"config/script",
		"config/recycle",
		"config/locale",
		"config/publish",
		"config/opt",
		"config/debug",
		"scripts",
	}

	var paths = []string{
		fmt.Sprintf(`config`),
		fmt.Sprintf(`config/core`),
		fmt.Sprintf(`config/core/业务`),
		fmt.Sprintf(`config/core/世界`),
		fmt.Sprintf(`config/core/资源`),

		fmt.Sprintf(`config/business`),
		fmt.Sprintf(`config/business/功能`),
		fmt.Sprintf(`config/business/区服`),
		fmt.Sprintf(`config/business/玩家`),
		fmt.Sprintf(`config/business/角色`),
		fmt.Sprintf(`config/business/地图`),
		fmt.Sprintf(`config/business/单位`),
		fmt.Sprintf(`config/business/物品`),
		fmt.Sprintf(`config/business/技能`),
		fmt.Sprintf(`config/business/其他`),

		fmt.Sprintf(`config/world`),
		fmt.Sprintf(`config/world/行为`),
		fmt.Sprintf(`config/world/技能`),
		fmt.Sprintf(`config/world/效果`),
		fmt.Sprintf(`config/world/武器`),
		fmt.Sprintf(`config/world/地块`),
		fmt.Sprintf(`config/world/场景`),
		fmt.Sprintf(`config/world/其他`),

		fmt.Sprintf(`config/asset`),
		fmt.Sprintf(`config/asset/地块`),
		fmt.Sprintf(`config/asset/装饰品`),
		fmt.Sprintf(`config/asset/模型`),
		fmt.Sprintf(`config/asset/纹理`),
		fmt.Sprintf(`config/asset/音乐`),
		fmt.Sprintf(`config/asset/音效`),
		fmt.Sprintf(`config/asset/预设`),
		fmt.Sprintf(`config/asset/图集`),

		fmt.Sprintf(`config/script`),
		fmt.Sprintf(`config/locale`),
		fmt.Sprintf(`config/publish`),
		fmt.Sprintf(`config/debug`),
		fmt.Sprintf(`config/texture`),
	}

	for _, path := range paths {
		path = models.Module.Get编辑器路径() + "/" + path
		_, oserr := os.Stat(path)
		if oserr != nil {
			if os.IsNotExist(oserr) {
				cerr := os.MkdirAll(path, os.ModePerm)
				if cerr != nil {
					return errorx.Wrap(cerr, "create file: "+path)
				}
			}
		}
	}

	return
}
